﻿/*
Copyright (c) 2010 The Developer Journey Company

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE SOFTWARE. 
*/

using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Device.Location;
using CommunityMegaphone.ViewModels;

namespace CommunityMegaphone
{
    public partial class App : Application
    {
        private static GeoCoordinateWatcher _geoWatcher = 
            new GeoCoordinateWatcher();
        private static GeoCoordinate _currentGeoLocation = 
            GeoCoordinate.Unknown;
        public static GeoCoordinate CurrentGeoLocation
        {
            get
            {
                return _currentGeoLocation;
            }
        }

        private static GeoPositionStatus _currentGeoStatus = 
            GeoPositionStatus.Disabled;
        public static GeoPositionStatus CurrentGeoStatus
        {
            get
            {
                return _currentGeoStatus;
            }
        }

        private static SearchResultsViewModel _searchResultsViewModel = null;
        public static SearchResultsViewModel SearchResultsViewModel
        {
            get
            {
                if (_searchResultsViewModel == null)
                    _searchResultsViewModel = new SearchResultsViewModel();

                return _searchResultsViewModel;
            }
        }

        private static ReferenceViewModel _referenceViewModel = null;
        public static ReferenceViewModel ReferenceViewModel
        {
            get
            {
                if (_referenceViewModel == null)
                {
                    _referenceViewModel = new ReferenceViewModel();
                    Application.LoadComponent(_referenceViewModel, new Uri(
                        "Data/ReferenceViewModel.xaml", UriKind.Relative));
                }
                return _referenceViewModel;
            }
        }

        private static CitiesViewModel _citiesViewModel = null;
        public static CitiesViewModel CitiesViewModel
        {
            get
            {
                if (_citiesViewModel == null)
                {
                    _citiesViewModel = new CitiesViewModel();
                    Application.LoadComponent(_citiesViewModel, new Uri(
                        "Data/CitiesViewModel.xaml", UriKind.Relative));
                }
                return _citiesViewModel;
            }
        }

        public PhoneApplicationFrame RootFrame { get; private set; }

        public App()
        {
            UnhandledException += Application_UnhandledException;

            InitializeComponent();
            InitializePhoneApplication();

            _geoWatcher.PositionChanged += GeoPositionChanged;
            _geoWatcher.StatusChanged += GeoStatusChanged;
        }

        private void GeoStatusChanged(object sender, 
            GeoPositionStatusChangedEventArgs e)
        {
            _currentGeoStatus = e.Status;
        }

        private void GeoPositionChanged(object sender, 
            GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            _currentGeoLocation = e.Position.Location;
        }

        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            _geoWatcher.Start(true);
        }

        private void Application_Activated(object sender, ActivatedEventArgs e)
        {
            _geoWatcher.Start(true);
        }

        private void Application_Deactivated(object sender, 
            DeactivatedEventArgs e)
        {
            _geoWatcher.Stop();
        }

        private void Application_Closing(object sender, ClosingEventArgs e)
        {
            _geoWatcher.Stop();
        }

        private void RootFrame_NavigationFailed(object sender, 
            NavigationFailedEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
                System.Diagnostics.Debugger.Break();
        }

        private void Application_UnhandledException(object sender, 
            ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
                System.Diagnostics.Debugger.Break();
        }

        private bool phoneApplicationInitialized = false;
        private void InitializePhoneApplication()
        {
            if (phoneApplicationInitialized)
                return;

            RootFrame = new PhoneApplicationFrame();
            RootFrame.Navigated += CompleteInitializePhoneApplication;
            RootFrame.NavigationFailed += RootFrame_NavigationFailed;
            phoneApplicationInitialized = true;
        }

        private void CompleteInitializePhoneApplication(object sender, 
            NavigationEventArgs e)
        {
            if (RootVisual != RootFrame)
                RootVisual = RootFrame;
            RootFrame.Navigated -= CompleteInitializePhoneApplication;
        }
    }
}
